import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:flutter_widgets_pc/resources/l10n/kq_intl.dart';
import 'package:flutter_widgets_pc/widgets/formItem/kq_empty_item_view.dart';

import '../../../resources/kq_theme_colors.dart';
import '../../../theme/kq_theme_manager.dart';
import '../../button/kq_ink_well.dart';
import '../../divider/kq_divider.dart';

/// 底部弹出-列表菜单
///
/// @author 周卓
///
class KqActionListBottomSheet extends StatelessWidget {
  /// 数据源
  final List<KqActionListData> items;

  /// 标题，不传则不显示
  final String? title;

  /// 是否显示取消按钮，默认true
  final bool showCancel;

  /// 选项文字对齐方式，默认居中
  final Alignment? alignment;

  /// 回调
  final Function(int index, KqActionListData item)? callback;

  /// 点击取消按钮回调
  final VoidCallback? onCancel;

  const KqActionListBottomSheet(
      {Key? key,
      required this.items,
      this.title,
      this.showCancel = true,
      this.callback,
      this.alignment,
      this.onCancel})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    // 小诸葛规范，最多显示6个半，多余滚动
    var maxHeight = 6.5 * 48;

    // 标题
    List<Widget> titleWidgets = [];

    if (title != null) {
      titleWidgets.add(Container(
        height: 40,
        alignment: Alignment.center,
        width: double.infinity,
        padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 20),
        child: Text(
          title!,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 12, color: KqThemeManager.getDialogConfig().titleColor?? KqThemeColors.text8C),
        ),
      ));
      titleWidgets.add(const KqDivider());
    }

    /// item
    for (int i = 0; i < items.length; i++) {
      var item = items[i];
      String title = item.item;
      children.add(KqInkWell(
        onTap: () {
          Navigator.pop(context);
          if (callback != null) {
            callback!(i, item);
          }
        },
        child: Container(
          height: 48,
          alignment: alignment ?? Alignment.center,
          width: double.infinity,
          padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 20),
          child: Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: 16, color: item.textColor ?? KqThemeManager.getDialogConfig().itemTextColor ?? KqThemeColors.text26),
          ),
        ),
      ));
      if (i < (items.length - 1)) {
        children.add(const KqDivider());
      }
    }

    // 取消按钮视图
    List<Widget> cancelWidgets = [];

    //取消按钮
    if (showCancel) {
      cancelWidgets.add(KqEmptyItemView(
        height: 8,
      ));
      cancelWidgets.add(KqInkWell(
        onTap: () {
          Navigator.pop(context);
          onCancel?.call();
        },
        child: Container(
          height: 48,
          alignment: Alignment.center,
          width: double.infinity,
          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 20),
          child: Text(
            KqIntl.currentResource.cancel,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: 16, color: KqThemeManager.getDialogConfig().cancelBtnColor ??KqThemeColors.text26),
          ),
        ),
      ));
    }

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    return UnconstrainedBox(
        child: SizedBox(
      width: context.width,
      child: Material(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8),
                  topRight: Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8))),
          color: KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
              KqThemeColors.bgWhite,
          child: Column(
            children: [
              ...titleWidgets,
              LimitedBox(
                maxHeight: maxHeight,
                child: ClipRRect(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(KqThemeManager.instance
                              .getConfig()
                              .dialogConfig
                              .radius ??
                          8),
                      topRight: Radius.circular(KqThemeManager.instance
                              .getConfig()
                              .dialogConfig
                              .radius ??
                          8)),
                  child: SingleChildScrollView(
                    child: dialogChild,
                  ),
                ),
              ),
              ...cancelWidgets,
            ],
          )),
    ));
  }
}

class KqActionListData {
  /// 选项标题
  final String item;

  /// 选项id，非必填
  final String? itemId;

  /// 选项文字颜色
  final Color? textColor;

  /// 自定义内容
  final dynamic extra;

  KqActionListData(
      {required this.item, this.itemId, this.textColor, this.extra});
}
